# -*- coding: utf-8 -*-

import json
import datetime

'''
    把数据库查询到的数据转换为json格式的工具类
'''


class MyJsonEncoder(json.JSONEncoder):
    '''
        重写构造json类，对日期等格式进行特殊处理
    '''

    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)


class Table2Json(object):
    '''
        工具类，把MySQL数据库获取的数据转换为JSON格式
    '''

    @staticmethod
    def user_list_to_json(userlist):
        data = []

        for row in userlist:
            result = {}
            result['user_id'] = row[0]
            result['user_type'] = row[1]
            result['login_name'] = row[2]
            result['login_phone'] = row[3]
            result['login_email'] = row[4]
            result['login_nickname'] = row[5]

            data.append(result)

        # 使用json.dumps将数据转换为json格式，json.dumps方法默认会输出成这种格式"\u5377\u76ae\u6298\u6263"，
        # 加ensure_ascii=False，则能够防止中文乱码。
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    '''个人用户表转JSON格式'''

    @staticmethod
    def puser_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['puser_id'] = row[0]
            result['puser_name'] = row[1]
            result['puser_job'] = row[2]
            result['puser_address'] = row[3]
            result['puser_email'] = row[4]
            result['puser_idea'] = row[5]
            result['puser_img'] = row[6]
            result['puser_code'] = row[7]
            result['puser_province'] = row[8]
            result['puser_city'] = row[9]
            result['puser_town'] = row[10]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''单位用户表转JSON格式'''

    @staticmethod
    def unit_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['unit_id'] = row[0]
            result['unit_name'] = row[1]
            result['unit_job'] = row[2]
            result['unit_address'] = row[3]
            result['unit_email'] = row[4]
            result['unit_busness'] = row[5]
            result['unit_img'] = row[6]

            result['unit_code'] = row[7]
            result['unit_province'] = row[8]
            result['unit_city'] = row[9]
            result['unit_town'] = row[10]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''项目行业表转JSON格式'''

    @staticmethod
    def indus_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['industry_id'] = row[0]
            result['industry_name'] = row[1]
            result['industry_vid'] = row[2]
            result['industry_code'] = row[3]
            result['industry_status'] = row[4]
            result['industry_sort'] = row[5]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''项目行业表转JSON格式'''

    @staticmethod
    def depart_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['depart_id'] = row[0]
            result['depart_name'] = row[1]
            result['depart_vid'] = row[2]
            result['depart_code'] = row[3]
            result['depart_status'] = row[4]
            result['depart_sort'] = row[5]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''项目行业表转JSON格式'''

    @staticmethod
    def item_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['item_id'] = row[0]
            result['industry_name'] = row[1]
            result['depart_name'] = row[2]
            result['handler_name'] = row[3]
            result['handler_phone'] = row[4]
            result['item_status'] = row[5]
            result['item_name'] = row[6]
            result['item_money'] = row[7]
            result['item_level'] = row[8]
            result['item_content'] = row[9]
            result['item_province'] = row[10]
            result['item_town'] = row[11]
            result['item_country'] = row[12]
            result['item_url'] = row[13]
            result['item_deadline'] = row[14]
            result['item_everyprice'] = row[15]
            result['classfy_pid'] = row[16]
            result['classfy_subname'] = row[17]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''历史搜索表转JSON格式'''

    @staticmethod
    def history_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['history_id'] = row[0]
            result['user_id'] = row[1]
            result['user_ip'] = row[2]
            result['history_keywords'] = row[3]
            result['history_type'] = row[4]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''历史搜索表转JSON格式'''

    @staticmethod
    def col_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['collection_id'] = row[0]
            result['user_id'] = row[1]
            result['item_id'] = row[2]
            result['collection_status'] = row[3]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_process表
    订单流程表
    '''

    @staticmethod
    def process_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['processId'] = row[0]
            result['processSum'] = row[1]
            result['processCode'] = row[2]
            result['processStatus'] = row[3]
            result['processName'] = row[4]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_prochild表
    订单子流程表
    '''

    @staticmethod
    def processChild_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['processChildId'] = row[0]
            result['processChildName'] = row[1]
            result['processChildCode'] = row[2]
            result['processChildStatus'] = row[3]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_message表
    消息表
    '''

    @staticmethod
    def message_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['messageId'] = row[0]
            result['messageType'] = row[1]
            result['messageName'] = row[2]
            result['messageTime'] = row[3]
            result['messageTitle'] = row[4]
            result['messageContent'] = row[5]
            result['userId'] = row[6]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_invoice
    发票管理表

    '''

    @staticmethod
    def invoice_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['invoiceId'] = row[0]
            result['invoiceType'] = row[1]
            result['invoiceContent'] = row[2]
            result['invoiceHeader'] = row[3]
            result['invoiceStatus'] = row[4]
            result['invoiceRemark'] = row[5]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_card
    银行卡管理表

    '''

    @staticmethod
    def card_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['cardId'] = row[0]
            result['userId'] = row[1]
            result['cardName'] = row[2]
            result['cardTel'] = row[3]
            result['cardType'] = row[4]
            result['cardCode'] = row[5]
            result['userType'] = row[6]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_indent
    项目订单表

    '''

    @staticmethod
    def indent_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['indentId'] = row[0]
            result['userId'] = row[1]
            result['indentSum'] = row[2]
            result['indentMessage'] = row[3]
            result['indentCode'] = row[4]
            result['indentStatus'] = row[5]
            result['invoiceId'] = row[6]
            result['itemId'] = row[7]
            result['processCode'] = row[8]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
   zzh_declar
   申报车表

    '''

    @staticmethod
    def declar_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['declarId'] = row[0]
            result['itemId'] = row[1]
            result['userId'] = row[2]
            result['declarStatus'] = row[3]
            result['serviceId'] = row[4]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
       zzh_hotsearch
       热搜表
       '''

    @staticmethod
    def hotsearch_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['hotsearchId'] = row[0]
            result['itemId'] = row[1]
            result['searchCount'] = row[2]
            result['searchStatus'] = row[3]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
    zzh_classfy
    分类表
    '''

    @staticmethod
    def classfy_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['classfyId'] = row[0]
            result['classfyName'] = row[1]
            result['classfyPid'] = row[2]
            result['classfyCode'] = row[3]
            result['classfyStatus'] = row[4]
            result['classfySort'] = row[5]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    '''
  zzh_project_publicity
  项目公示表
    '''

    @staticmethod
    def projectPublicity_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['projectPublicityId'] = row[0]
            result['publicityTitle'] = row[1]
            result['departId'] = row[2]
            result['publishTime'] = row[3]
            result['publicityUrl'] = row[4]
            result['publicityStatus'] = row[5]
            result['publicityRemark'] = row[6]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    # 为分类页面载入的时候请求第一级别的分类。对应着那16个一级分类ID和一级分类Name，TableJSON
    @staticmethod
    def classfy_name_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['classfyName'] = row[0]
            result['classfyPcode'] = row[1]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData



    @staticmethod
    def indent_and_item_to_json(dataList):
        '''
        indent 表和 item 表综合查询
        '''
        data = []
        for row in dataList:
            result = {}
            result['indentId'] = row[0]
            result['userId'] = row[1]
            result['indentSum'] = row[2]
            result['indentMessage'] = row[3]
            result['indentCode'] = row[4]
            result['indentStatus'] = row[5]
            result['invoiceId'] = row[6]
            result['itemId'] = row[7]
            result['processCode'] = row[8]
            result['indentTime'] = row[9]
            result['industryName'] = row[11]
            result['departName'] = row[12]
            result['handlerName'] = row[13]
            result['handlerPhone'] = row[14]
            result['itemStatus'] = row[15]
            result['itemName'] = row[16]
            result['itemMoney'] = row[17]
            result['itemLevel'] = row[18]
            result['itemContent'] = row[19]
            result['itemProvince'] = row[20]
            result['itemTown'] = row[21]
            result['itemCountry'] = row[22]
            result['itemUrl'] = row[23]
            result['deadline'] = row[24]
            result['itemEveryprice'] = row[25]
            result['classfyPid'] = row[26]
            result['classfySubname'] = row[27]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def collection_and_item_to_json(dataList):
        '''
        collection 表和 item 表综合查询
        '''
        data = []
        for row in dataList:
            result = {}
            result['collectionId'] = row[0]
            result['userId'] = row[1]
            result['itemId'] = row[2]
            result['collectionStatus'] = row[3]

            result['industryName'] = row[5]
            result['departName'] = row[6]
            result['handlerName'] = row[7]
            result['handlerPhone'] = row[8]
            result['itemStatus'] = row[9]
            result['itemName'] = row[10]
            result['itemMoney'] = row[11]
            result['itemLevel'] = row[12]
            result['itemContent'] = row[13]
            result['itemProvince'] = row[14]
            result['itemTown'] = row[15]
            result['itemCountry'] = row[16]
            result['itemUrl'] = row[17]
            result['deadline'] = row[18]
            result['itemEveryprice'] = row[19]
            result['classfyPid'] = row[20]
            result['classfySubname'] = row[21]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def declar_and_item_to_json(dataList):
        """
        declar表和item表综合查询
        :param dataList:
        :return: json格式数据
        """
        data = []
        for row in dataList:
            result = {}
            result['declarId'] = row[0]
            result['itemId'] = row[1]
            result['userId'] = row[2]
            result['declarStatus'] = row[3]
            result['serviceId'] = row[4]

            result['industryName'] = row[6]
            result['departName'] = row[7]
            result['handlerName'] = row[8]
            result['handlerPhone'] = row[9]
            result['itemStatus'] = row[10]
            result['itemName'] = row[11]
            result['itemMoney'] = row[12]
            result['itemLevel'] = row[13]
            result['itemContent'] = row[14]
            result['itemProvince'] = row[15]
            result['itemTown'] = row[16]
            result['itemCountry'] = row[17]
            result['itemUrl'] = row[18]
            result['deadline'] = row[19]
            result['itemEveryprice'] = row[20]
            result['classfyPid'] = row[21]
            result['classfySubname'] = row[22]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def classfy_and_item_to_json(dataList):
        """
        classfy表和item表综合查询
        :param dataList:
        :return: json格式数据
        """
        data = []
        for row in dataList:
            result = {}
            result['item_id'] = row[0]
            result['industry_name'] = row[1]
            result['depart_name'] = row[2]
            result['handler_name'] = row[3]
            result['handler_phone'] = row[4]
            result['item_status'] = row[5]
            result['item_name'] = row[6]
            result['item_money'] = row[7]
            result['item_level'] = row[8]
            result['item_content'] = row[9]
            result['item_province'] = row[10]
            result['item_town'] = row[11]
            result['item_country'] = row[12]
            result['item_url'] = row[13]
            result['item_deadline'] = row[14]
            result['item_everyprice'] = row[15]
            result['classfy_pid'] = row[16]
            result['classfy_subname'] = row[17]

            result['classfyId'] = row[18]
            result['classfyName'] = row[19]
            result['classfyPid'] = row[20]
            result['classfyCode'] = row[21]
            result['classfyStatus'] = row[22]
            result['classfySort'] = row[23]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def colletionIdAndStatus_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['collectionId'] = row[0]
            result['collectionStatus'] = row[1]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def declarIdAndStatus_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['declarId'] = row[0]
            result['declarStatus'] = row[1]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def indentIdAndStatus_list_to_json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['indentId'] = row[0]
            result['indentStatus'] = row[1]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def complexList(list1,list2,list3):
        data1 = []
        for row in list1:
            result = {}
            result['departName'] = row[0]
            result['departCode'] = row[1]
            data1.append(result)
        departJson = json.dumps(data1, cls=MyJsonEncoder, ensure_ascii=False)

        data2 = []
        for row in list2:
            result = {}
            result['industryName'] = row[0]
            result['industryCode'] = row[1]
            data2.append(result)
        indusJson = json.dumps(data2, cls=MyJsonEncoder, ensure_ascii=False)

        data3 = []
        for row in list3:
            result = {}
            result['handlerName'] = row[0]
            data3.append(result)
        handJson = json.dumps(data3, cls=MyJsonEncoder, ensure_ascii=False)
        return {"departInfo":departJson,"industryInfo":indusJson,"handlerInfo":handJson}

    @staticmethod
    def indus2json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['industryName'] = row[0]
            result['industryCode'] = row[1]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def depart2json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['departName'] = row[0]
            result['departCode'] = row[1]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData

    @staticmethod
    def hand2json(dataList):
        data = []
        for row in dataList:
            result = {}
            result['handlerName'] = row[0]
            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)
        return jsonData